    //
//  ICEFullScreenTextVC.m
//  ICEViewPrototype
//
//  Created by Aurélien Ammeloot on 14/07/2010.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ICEFullScreenTextVC.h"
#import "ICEFontListViewController.h"

@interface ICEFullScreenTextVC () <ICEFontListViewControllerDelegate, UIPopoverControllerDelegate>

- (void)_pinch:(id)sender;

@end


@implementation ICEFullScreenTextVC

@synthesize delegate = _delegate;

CGFloat initialFontSize;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
 
	_pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(_pinch:)];
	[_textView addGestureRecognizer:_pinchGestureRecognizer];
 
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

	
    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[_list release];
	[_pinchGestureRecognizer release];
	
    [super dealloc];
}

#pragma mark -
#pragma mark Event Handling

- (IBAction)dissmiss:(id)sender
{
	[_delegate fullScreenTextVCRequestsDismissal:self];
}

- (IBAction)enlargeFont:(id)sender
{
	UIFont *font = [_textView font];
	font = [font fontWithSize:font.pointSize+1.];
	[_textView setFont:font];
}

- (IBAction)reduceFont:(id)sender
{
	UIFont *font = [_textView font];
	font = [font fontWithSize:font.pointSize-1.];
	[_textView setFont:font];
}

- (IBAction)showFontList:(id)sender
{
	if(_list == nil) {
		_list = [[ICEFontListViewController alloc] initWithStyle:UITableViewStyleGrouped];
		_list.delegate = self;
	}
	
	UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:_list];
	[popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

#pragma mark -
#pragma mark ICEFontListViewController delegate

- (void)fontListViewController:(ICEFontListViewController *)fontListViewController DidReturnFontName:(NSString *)fontName
{
	UIFont *font = [_textView font];
	font = [UIFont fontWithName:fontName size:font.pointSize];
	[_textView setFont:font];
}

#pragma mark -
#pragma mark UIPopoverControllerDelegate

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
	[popoverController release];
}

#pragma mark -
#pragma mark UIGestureRecognizer
- (void)_pinch:(UIPinchGestureRecognizer *)sender
{
//	NSLog(@"scale: %f",sender.scale);
//	NSLog(@"velocity: %f", sender.velocity);
	
	if (sender.state == UIGestureRecognizerStateBegan) {
		initialFontSize = _textView.font.pointSize;
	}
	if (sender.state == UIGestureRecognizerStateChanged) {
		UIFont *font = [_textView font];
		font = [font fontWithSize:initialFontSize + sender.scale];
		[_textView setFont:font];
	}
}

@end
